package cc.linker.steplink.crm.merchant.controller.acs;

import cc.linker.steplink.crm.commons.http.HttpService;
import cc.linker.steplink.crm.commons.model.PcUserInfo;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.merchant.service.acs.BindBankService;
import cc.linker.steplink.crm.merchant.util.NumberCheckUtil;
import cc.linker.steplink.crm.response.GlobalException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bank")
public class BindBankController {

    @Autowired
    private BindBankService bindBankService;
    @Autowired
    private HttpService httpService;
    @Autowired
    private UserInfoUtil userInfoUtil;


    /**
     * 用户没有绑卡，页面显示用户的开户名称
     * @param session
     */
    @RequestMapping("/checkbindstatus")
    public String checkBindStatus(HttpServletRequest request) throws GlobalException {
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        //查询权限认证中的用户casUserId
        String casUserId = userinfo.getAdminId();
        String customerId = bindBankService.findCustomerId(casUserId);
        String corpName=null;
        if (!StringUtils.isEmpty(customerId)){
            //这里返回企业名称
            corpName = bindBankService.findCorpName(customerId);
        }
        return corpName;
    }

    /**
     * 点击下一步，用户在这里绑卡，并提交
     * @param param
     */
    @RequestMapping("/bindbank")
    public void bindBank(@RequestBody Map<String,String> param,HttpServletRequest request) throws GlobalException {
        if (null!=param){
            PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
            //获取用户输入的银行卡号
            String bankAccountNumber = param.get("bankAccountNumber");
            if (StringUtils.isEmpty(bankAccountNumber)){
                throw GlobalException.build("请输入银行卡号");
            }
            //银行卡号有效性校验
            boolean flag = NumberCheckUtil.isBankCardNum(bankAccountNumber);
            if (flag){
                String corpName=null;
                //查询权限认证中的用户casUserId
                String casUserId = userinfo.getAdminId();
                if (!StringUtils.isEmpty(casUserId)){
                    //获取当前登录用户的企业名称
                    String customerId = bindBankService.findCustomerId(casUserId);
                    if (!StringUtils.isEmpty(customerId)){
                        corpName = bindBankService.findCorpName(customerId);
                    }
                }
                //获取用户选择的银行名称
                String bankAccountName = param.get("bankAccountName");
                if (StringUtils.isEmpty(bankAccountName)){
                    throw GlobalException.build("请选择开户银行");
                }
                //获取用户所选银行的ID 没有通过银行卡号判断所属银行，而是通过银行查询该银行的ID
                String bankID = bindBankService.findBankID(bankAccountName);
                if (!StringUtils.isEmpty(bankID)){
                    //TODO `BANK_CARDTYPE` varchar(2) DEFAULT NULL COMMENT '卡类型,10=借记账户,20=贷记账户',
                    //卡类型未知
                    Map<Object, Object> map = new HashMap<>();
                    if (!StringUtils.isEmpty(corpName)){
                        map.put("corporationName",corpName);
                    }
                    map.put("bankID",bankID);
                    map.putAll(param);
                    //TODO
                    //这里要把封装好的数据发送给中金，暂无
                    //httpService.doPost()
                }
            }else {
                throw GlobalException.build("您输入的银行卡号有误，请确认后输入正确的卡号");
            }
        }
    }

    /**
     * 返回用户绑定的所有银行卡
     * @param session
     * @return
     */
    @RequestMapping("/returnbanklist")
    public List<Map<String,String>> returnBankList(HttpServletRequest request){
        PcUserInfo userinfo = this.userInfoUtil.getPcUserInfo(request);
        String customerId = bindBankService.findCustomerId(userinfo.getAdminId());
        List<Map<String, String>> returnbanklist=null;
        if (!StringUtils.isEmpty(customerId)){
            returnbanklist = bindBankService.returnbanklist(customerId);
        }
        return returnbanklist;
    }


}
